package com.FIlter;

import com.entry.User;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = {"/Pformation/*","/PSales/returall.jsp","/PSales/return.jsp","/system/sup.jsp","/cg_manage/*","/man_manage/*"})
public class AdminFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            session.setAttribute("userLoginMess","请先登录");
            response.sendRedirect("/QY/index.jsp");
        }else {
            if (session != null && "管理员".equals(user.getUsergroup())) {
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                session.setAttribute("userLoginMess", "请登录管理员账号");
                response.sendRedirect("/QY/index.jsp");
            }
        }
    }

    @Override
    public void destroy() {

    }
}
